﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace PropertyGrid控件
{
    public class ShowedClass
    {
        [DisplayName("名称")]
        [Editor(typeof(Editor), typeof(System.Drawing.Design.UITypeEditor))]
        public string Name { get; set; }

        [Editor(typeof(Editor), typeof(System.Drawing.Design.UITypeEditor))]
        public string Description { get; set; }
    }

    public partial class EditorControl : UserControl
    {
        private string _result = "";

        public EditorControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 文本内容
        /// </summary>
        public string Result { get => comboBox1.SelectedItem.ToString(); }
    }

    public class Editor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            // 编辑属性值时，在右侧显示...更多按钮
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            var edSvc = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
            if (edSvc != null)
            {
                var popedControl = new EditorControl();
                // 还有ShowDialog这种方式，可以弹出一个窗体来进行编辑
                edSvc.DropDownControl(popedControl);
                value = popedControl.Result;
            }
            return base.EditValue(context, provider, value);
        }
    }

}
